import TextureRenderUtils from "../TextureRenderUtils";
import Resolution from "../Resolution";
const { ccclass, property } = cc._decorator;

@ccclass
/**
 * 基础的绘画类，不会使用RenderTexture更新内容
 * 纯绘制图形
 */
export default class BaseDraw extends cc.Component {

    @property(cc.Camera)
    camera: cc.Camera = null;

    @property(cc.Graphics)
    g: cc.Graphics = null;

    //最后需要这个node生成图片需要的数据，用于保存
    @property(cc.Node)
    renderNode: cc.Node = null;

    @property
    radius: number = 30;

    //画笔的颜色
    protected color: cc.Color;
    //辅助类 
    protected textureHelper: TextureRenderUtils = new TextureRenderUtils();

    protected tempBuffer: cc.Vec2[] = [];
    start() {
        this.color = this.g.fillColor;
        this.camera.zoomRatio *= Resolution.getFDRatio()
        // console.log("BaseDraw  this.camera.zoomRatio ", this.camera.zoomRatio)
        this.textureHelper.init(this.camera, this.renderNode)
    }

    onEnable() {

    }

    getTextureInfo() {
        return this.textureHelper.getTextureInfo()
    }


    get width() {
        return this.renderNode.width;
    }

    get height() {
        return this.renderNode.height
    }




    //获得绘制的
    getDataUrl() {
        let data = this.textureHelper.getDataUrl();
        return data;
    }

    get fomart() {
        return this.textureHelper.getPixelFormat()
    }

    paintStart(pos: cc.Vec2) {
        // this.tempBuffer.push(pos)
        this.drawing(pos)
    }

    clear() {
        this.tempBuffer.length = 0;
        this.g.clear()
    }




    getData() {
        this.textureHelper.render()
        return this.textureHelper.getData()
    }

    drawing(element) {

        let g = this.g;
        // g.lineWidth = 10;
        // for (let index = 0; index < posList.length; index++) {
        //     const element = posList[index];
        let x = element.x;
        let y = element.y;
        g.moveTo(x, y)
        g.circle(x, y, this.radius)
        // }
        g.close();
        // g.stroke();
        g.fill();
    }

    setColor(color: cc.Color) {
        this.color = color;
        this.g.fillColor = color;
        this.g.strokeColor = color;
    }


}
